package com.ymt.bpm.util;

import org.springframework.util.Base64Utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;

/**
 * Created by Johnny on 2017/10/11.
 */
public class EncodeUtil {

    public static String decodeBase64(String src) {
        if (src==null || src.length()==0) {
            return src;
        }
        try {
            return new String(Base64Utils.decodeFromString(src), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decodeEncodedBase64(String src) {
        if (src==null || src.length()==0) {
            return src;
        }
        try {
            return URLDecoder.decode(decodeBase64(src), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decode(String src) {
        if (src==null || src.length()==0) {
            return src;
        }
        try {
            return URLDecoder.decode(src, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encodeBase64(String src) {
        if (src==null || src.length()==0) {
            return src;
        }
        return Base64Utils.encodeToString(src.getBytes(Charset.forName("UTF-8")));
    }

}
